adTempus API
Client API / Examples / Create a Simple Job
In This Topic
    Create a Simple Job
    In This Topic

    This sample demonstrates how to create a simple job that runs a program on November 1 at 10:00 am.

    Be sure to change the user name in the line that fetches the Credential Profile so that you are retrieving a valid profile on your server.

    'Connect to the local adTempus server, using Windows authentication.
    Using connection As Scheduler = Scheduler.Connect()
    
        'Create a DataContext to work in. All object operations take place within this context.
        'Use a Using block so the context is disposed when we finish with it
        Using context As DataContext = connection.NewDataContext()
            'Get the CredentialProfile for user "sampledomain\sampleuser"
            'TODO: Change this to a valid Credential Profile on your server.
            Dim credentials As CredentialProfile = context.GetCredentialProfile("sampledomain\sampleuser")
    
            'fetch the root job group (the "Jobs" node in the Console)
            '(specifying Nothing for the group name in GetJobGroup returns the root group).
            Dim rootGroup As JobGroup = context.GetJobGroup(Nothing)
    
            'Create a new job within the root group.
            'The job will be assigned to the "Default" Job Queue automatically.
            Dim job As Job = rootGroup.NewJob()
    
            job.Name = "My Test Job"
            job.UserInteractionMode = UserInteractionMode.Hidden
    
            'assign the Credential Profile we already fetched.
            job.Credentials = credentials
    
            'we will create a step to run the "adtexec" utility installed with adTempus
    
            Dim task As ProgramExecutionTask = CType(context.CreateObject(ClassID.ProgramExecutionTask), ProgramExecutionTask)
    
            'Run the adtexec program. The "ADTServerPath" variable will be replaced by the server at execution time with the path where adTempus is installed
            task.ExecutionTarget = "%ADTServerPath%\adtexec.exe"
    
            'send the "/list" command-line option
            task.CommandLineParameters = "/list"
    
            'Capture the screen output from the program
            task.ConsoleCaptureOptions = ConsoleCaptureOptions.CaptureConsole
    
            'assign the task to a Step
            Dim step1 As JobStep = CType(context.CreateObject(ClassID.JobStep), JobStep)
            step1.Task = task
    
            'add the step to the job
            job.Steps.Add(step1)
    
    
            'create a trigger to run this job every day at 10:00 am on November 1
            Dim trigger As ScheduleTrigger = CType(context.CreateObject(ClassID.ScheduleTrigger), ScheduleTrigger)
            Dim schedule As Schedule = CType(context.CreateObject(ClassID.Schedule), ArcanaDevelopment.adTempus.Client.Schedule)
    
    
            Dim dateCriterion As DateCriterion = CType(context.CreateObject(ClassID.DateCriterion), ArcanaDevelopment.adTempus.Client.DateCriterion)
            dateCriterion.CriterionType = DateCriterionType.SpecifiedDays
    
            'criterion runs on selected days of selected months
            Dim dayRule As DaySpecification = CType(context.CreateObject(ClassID.DaySpecification), DaySpecification)
            dayRule.SpecificationType = DaySpecificationType.SpecifiedDays
            'select month 11 (November)
            dayRule.SetMonth(11, True)
    
            'select day 1
            dayRule.SetDay(1, True)
    
            'add the rule to the criterion
            dateCriterion.Days.Add(dayRule)
    
            'add the criterion to the schedule
            schedule.DateCriterion = dateCriterion
    
            'create a time criterion to run at the desired time
            Dim timeCriterion As TimeCriterion = CType(context.CreateObject(ClassID.TimeCriterion), ArcanaDevelopment.adTempus.Client.TimeCriterion)
            timeCriterion.CriterionType = TimeCriterionType.SpecifiedTimes
            'assign the time "10:00am" (the date portion of the DateTime object is ignored)
            timeCriterion.Times.Add(New DateTime(2000, 1, 1, 10, 0, 0))
    
            'assign the time criterion to the schedule
            schedule.TimeCriterion = timeCriterion
    
            'now add the schedule to the trigger
            trigger.Schedules.Add(schedule)
    
            'and finally add the trigger to the job
            job.Triggers.Add(trigger)
    
    
            'Save the job
            'The job will be validated on the server, and the save will fail if validation fails.
            'In that case, errors will be returned in the messages collection.
            Dim messages As MessageCollection = Nothing
            If Not job.Save(messages) Then
                For Each Message As Message In messages
                    Debug.WriteLine(Message.ToString())
                Next
            End If
        End Using
    End Using
    
    //Connect to the local adTempus server, using Windows authentication.
    using (Scheduler connection = Scheduler.Connect())
    {
        //Create a DataContext to work in. All object operations take place within this context.
        //Use a Using block so the context is disposed when we finish with it
        using (DataContext context = connection.NewDataContext())
        {
            //Get the CredentialProfile for user "sampledomain\sampleuser"
            //TODO: Change this to a valid Credential Profile on your server.
            CredentialProfile credentials = context.GetCredentialProfile("sampledomain\\sampleuser");
    
            //fetch the root job group (the "Jobs" node in the Console)
            //(specifying Nothing for the group name in GetJobGroup returns the root group).
            JobGroup rootGroup = context.GetJobGroup(null);
    
            //Create a new job within the root group.
            //The job will be assigned to the "Default" Job Queue automatically.
            Job job = rootGroup.NewJob();
    
            job.Name = "My Test Job";
            job.UserInteractionMode = UserInteractionMode.Hidden;
    
            //assign the Credential Profile we already fetched.
            job.Credentials = credentials;
    
            //we will create a step to run the "adtexec" utility installed with adTempus
    
            ProgramExecutionTask task = (ProgramExecutionTask)context.CreateObject(ClassID.ProgramExecutionTask);
    
            //Run the adtexec program. The "ADTServerPath" variable will be replaced by the server at execution time with the path where adTempus is installed
            task.ExecutionTarget = "%ADTServerPath%\\adtexec.exe";
    
            //send the "/list" command-line option
            task.CommandLineParameters = "/list";
    
            //Capture the screen output from the program
            task.ConsoleCaptureOptions = ConsoleCaptureOptions.CaptureConsole;
    
            //assign the task to a Step
            JobStep step1 = (JobStep)context.CreateObject(ClassID.JobStep);
            step1.Task = task;
    
            //add the step to the job
            job.Steps.Add(step1);
    
    
            //create a trigger to run this job every day at 10:00 am on November 1
            ScheduleTrigger trigger = (ScheduleTrigger)context.CreateObject(ClassID.ScheduleTrigger);
            Schedule schedule = (ArcanaDevelopment.adTempus.Client.Schedule)context.CreateObject(ClassID.Schedule);
    
    
            DateCriterion dateCriterion = (ArcanaDevelopment.adTempus.Client.DateCriterion)context.CreateObject(ClassID.DateCriterion);
            dateCriterion.CriterionType = DateCriterionType.SpecifiedDays;
    
            //criterion runs on selected days of selected months
            DaySpecification dayRule = (DaySpecification)context.CreateObject(ClassID.DaySpecification);
            dayRule.SpecificationType = DaySpecificationType.SpecifiedDays;
            //select month 11 (November)
            dayRule.SetMonth(11, true);
    
            //select day 1
            dayRule.SetDay(1, true);
    
            //add the rule to the criterion
            dateCriterion.Days.Add(dayRule);
    
            //add the criterion to the schedule
            schedule.DateCriterion = dateCriterion;
    
            //create a time criterion to run at the desired time
            TimeCriterion timeCriterion = (ArcanaDevelopment.adTempus.Client.TimeCriterion)context.CreateObject(ClassID.TimeCriterion);
            timeCriterion.CriterionType = TimeCriterionType.SpecifiedTimes;
            //assign the time "10:00am" (the date portion of the DateTime object is ignored)
            timeCriterion.Times.Add(new DateTime(2000, 1, 1, 10, 0, 0));
    
            //assign the time criterion to the schedule
            schedule.TimeCriterion = timeCriterion;
    
            //now add the schedule to the trigger
            trigger.Schedules.Add(schedule);
    
            //and finally add the trigger to the job
            job.Triggers.Add(trigger);
    
    
            //Save the job
            //The job will be validated on the server, and the save will fail if validation fails.
            //In that case, errors will be returned in the messages collection.
            MessageCollection messages = null;
            if (!job.Save(out messages))
            {
                foreach (Message Message in messages)
                {
                    System.Diagnostics.Debug.WriteLine(Message.ToString());
                }
            }
        }
    }
    
    See Also